package com.soulkun.smarthome.ui.main.personal.dialog

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppDialogMainPersonalFamilyRenameBinding
import com.soulkun.smarthome.databinding.AppDialogMainPersonalRenameBinding
import com.soulkun.smarthome.ui.main.personal.viewmodel.*
import soulkun.library.mvvm.component.BaseDataBindingDialogFragment
import soulkun.library.utils.getAppContext
import soulkun.library.utils.toPx
import soulkun.library.utils.toast

class AppMainPersonalFamilyRenameDialog(private val dataViewModel: AppMainPersonalFamilySettingViewModel) : BaseDataBindingDialogFragment<AppDialogMainPersonalFamilyRenameBinding>() {

    private val viewModel by viewModels<AppMainPersonalFamilyRenameViewModel> { defaultViewModelProviderFactory }

    override fun setLayout(): Int {
        return R.layout.app_dialog_main_personal_family_rename
    }

    override fun doOnViewCreated() {
        requireDialog().window?.run {
            setLayout(requireContext().resources.getDimensionPixelSize(R.dimen.sw_300dp), ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        isCancelable = false
        requireDataBinding().lifecycleOwner = viewLifecycleOwner
        requireDataBinding().viewModel = viewModel
        requireDataBinding().onClickListener = View.OnClickListener {
            when (it.id) {
                R.id.iv_main_personal_family_rename_icon_delete -> {
                    viewModel.familyNameEditLiveData.value = ""
                }
                R.id.tv_main_personal_family_rename_text_cancel -> {
                    dismissAllowingStateLoss()
                }
                R.id.tv_main_personal_family_rename_text_save -> {
                    dataViewModel.modifyFamilyName(viewModel.familyNameEditLiveData.value!!){
                        dismissAllowingStateLoss()
                    }
                }
            }
        }
    }

    override fun doOnResume() {
        requireDataBinding().etMainPersonalFamilyRenameTextFamilyName.run {
            requestFocus()
            setText(dataViewModel.familyNameLiveData.value!!)
        }
    }

    override fun doOnDismiss() {
    }

    override fun doOnDestroyView() {
    }

}